package com.luofan.controller;


import com.github.pagehelper.PageInfo;
import com.luofan.common.result.Result;
import com.luofan.common.result.ResultCodeEnum;
import com.luofan.entity.dto.ArchiveQueryDTO;
import com.luofan.entity.po.Archive;
import com.luofan.entity.vo.ArchiveVO;
import com.luofan.entity.vo.StatisticsVO;
import com.luofan.service.ArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 档案表 前端控制器
 * </p>
 *
 * @author 
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/archive")
public class ArchiveController {

    @Autowired
    private ArchiveService archiveService;

    //教师上传档案
    @PostMapping
    public Result<String> saveArchiveByCollectId(@RequestBody Archive archive) {
        ResultCodeEnum resultCodeEnum;
        try {
            resultCodeEnum = archiveService.saveArchiveByCollectId(archive);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
        return Result.build(null, resultCodeEnum);
    }

    //教师修改档案(已通过审核不能修改)
    @PutMapping
    public Result<String> updateArchiveByCollectId(@RequestBody Archive archive) {
        ResultCodeEnum resultCodeEnum = archiveService.updateArchiveById(archive);
        return Result.build(null, resultCodeEnum);
    }

    //根据档案收集任务id和用户id获取档案信息
    @GetMapping("/{collectId}/{userId}")
    public Result<ArchiveVO> getArchiveByCollectIdAndUserId(@PathVariable Long collectId, @PathVariable Long userId) {
        ArchiveVO archiveVO = archiveService.getArchiveByCollectIdAndUserId(collectId, userId);
        return Result.ok(archiveVO);
    }

    //教研室主任 审核档案(审核通过需要评分)
    @PutMapping("/check")
    public Result<String> checkArchive(@RequestBody Archive archive) {
        ResultCodeEnum resultCodeEnum = archiveService.checkArchive(archive);
        return Result.build(null, resultCodeEnum);
    }

    //根据条件 分页获取档案信息
    @GetMapping("/condition")
    public Result<PageInfo<ArchiveVO>> listArchiveByCondition(ArchiveQueryDTO archiveQueryDTO) {
        PageInfo<ArchiveVO> pageInfo = archiveService.listArchiveByCondition(archiveQueryDTO);
        return Result.ok(pageInfo);
    }

    //根据教师id 获取档案的平均提交所需时间，逾期提交率，平均质量评分
    @GetMapping("/statistics/user/{userId}")
    public Result<StatisticsVO> statisticsByUserId(@PathVariable Long userId) {
        StatisticsVO statisticsVO = archiveService.statisticsByUserId(userId);
        return Result.ok(statisticsVO);
    }

    //根据档案收集任务id 获取档案的平均提交所需时间，逾期提交率，平均质量评分
    @GetMapping("/statistics/collect/{collectId}")
    public Result<StatisticsVO> statisticsByCollectId(@PathVariable Long collectId) {
        StatisticsVO statisticsVO = archiveService.statisticsByCollectId(collectId);
        return Result.ok(statisticsVO);
    }

    //根据教研室id和学期id 获取档案的平均提交所需时间，逾期提交率，平均质量评分
    @GetMapping("/statistics/section/{sectionId}/{termId}")
    public Result<StatisticsVO> statisticsBySectionIdAndTermId(@PathVariable Long sectionId, @PathVariable Long termId) {
        StatisticsVO statisticsVO = archiveService.statisticsBySectionIdAndTermId(sectionId, termId);
        return Result.ok(statisticsVO);
    }

    //根据学期id 获取档案的平均提交所需时间，逾期提交率，平均质量评分
    @GetMapping("/statistics/term/{termId}")
    public Result<StatisticsVO> statisticsByTermId(@PathVariable Long termId) {
        StatisticsVO statisticsVO = archiveService.statisticsByTermId(termId);
        return Result.ok(statisticsVO);
    }


}
